home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / dualplay.c < prev    next >
C/C++ Source or Header  |  1985-10-30  |  10KB  |  323 lines

  1. /****************************************************************
  2. *                                                               *
  3. * Copyright 1985, Commodore Amiga Inc.  All rights reserved.    *
  4. * No part of this program may be reproduced, transmitted,       *
  5. * transcribed, stored in retrieval system, or translated into   *
  6. * any language or computer language, in any form or by any      *
  7. * means, electronic, mechanical, magnetic, optical, chemical,   *
  8. * manual or otherwise, without the prior written permission of  *
  9. * Commodore Amiga Incorporated, 983 University Ave, #D          *
  10. * Los Gatos, CA 95030                                           *
  11. *                                                               *
  12. ****************************************************************/
  13.  
  14. /* dual playfield in which the background area is larger than the foreground
  15.  * and a scrolling effect is used to move the larger one's perspective */
  16.  
  17.  
  18.  
  19.  
  20. #include <exec/types.h>
  21. #include <graphics/gfx.h>
  22. #include <hardware/dmabits.h>
  23. #include <hardware/custom.h>
  24. #include <graphics/gfxmacros.h>
  25. #include <graphics/rastport.h>
  26. #include <graphics/view.h>
  27. #include <exec/exec.h>
  28.  
  29. #include <graphics/copper.h>
  30.  
  31. #define MAXSCROLL_X 40
  32. #define MAXSCROLL_Y 20
  33. #define DEPTH 2  
  34. #define WIDTH 320 
  35. #define HEIGHT 200 
  36. #define WIDTH1 360 
  37. #define HEIGHT1 220 
  38. #define NOT_ENOUGH_MEMORY -1000
  39. #define FOREVER for(;;) 
  40.         /* construct a simple display */ 
  41.  
  42. struct View v;
  43. struct ViewPort vp;
  44. struct ColorMap *cm;    /* pointer to colormap structure, dynamic alloc */
  45. struct RasInfo ri;
  46. struct BitMap b;
  47. struct RastPort rp;
  48.  
  49. /* added a second RasInfo for dual.playfield */
  50. struct RasInfo ri2;
  51. /* added a second BitMap for dual.playfield */
  52. struct BitMap b2;
  53.  
  54. short i,j,k,n;
  55. struct ColorMap *GetColorMap();
  56. struct GfxBase *GfxBase;
  57.  
  58. SHORT  boxoffsets[] = { 786, 1986, 3194 };
  59. USHORT colortable[] = { 0x000, 0xf00, 0x0f0, 0x00f,
  60.                         0,0,0,0,                
  61.                         0,     0x4f8, 0x62a, 0xf9c };
  62.  
  63.                         /* black, red, green, blue, 
  64.                            ignored, ignored, ignored, ignored,
  65.                            (transparent), purple, lime green, mauve */
  66. /* nobody will see center set of 4 colors in this case since only two planes
  67.  * and dualplayfield mode. */
  68.  
  69. /* added a second set of box offsets for dual.playfield */
  70. SHORT boxoffsets2[] = { 2803, 1608, 415 };
  71.  
  72. UBYTE *displaymem,*displaymem2;
  73. UWORD *colorpalette;
  74.  
  75. /* assume depth is the same for both playfields, just for simple example */
  76.  
  77. main()
  78. {
  79.         GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  80.         if (GfxBase == NULL) exit(1);
  81.  
  82.                                 /* initialize view */
  83.         InitView(&v);
  84.                                 /* link view into viewport */
  85.         v.ViewPort = &vp;
  86.                                 /* init view port */
  87.         InitVPort(&vp);
  88.                                 /* now specify critical characteristics */
  89.         vp.DWidth = WIDTH;
  90.         vp.DHeight = HEIGHT;
  91.         vp.RasInfo = &ri;
  92.         vp.Modes = DUALPF | PFBA ;      /* DUAL PLAYFIELD MODE */
  93.  
  94.                                 /* init bit map (for rasinfo and rastport) */
  95.         InitBitMap(&b,DEPTH,WIDTH1,HEIGHT1);
  96.                                 /* (init RasInfo) */
  97.         ri.BitMap = &b;
  98.         ri.RxOffset = 0;        /* align upper left corners of display
  99.                                  * with upper left corner of drawing area */
  100.         ri.RyOffset = 0;
  101.  
  102. /* ********************************************************************* */
  103. /* changed here for dual.playfield */
  104.         InitBitMap(&b2,DEPTH,WIDTH,HEIGHT);
  105.         ri.Next = &ri2;
  106.         ri2.BitMap = &b2;
  107.         ri2.RxOffset = 0;
  108.         ri2.RyOffset = 0;
  109.         ri2.Next = 0;
  110. /* ********************************************************************* */
  111.  
  112.                                 /* (init color table) */
  113.         cm = GetColorMap(12);   /* 12 entries, since Dual Playfield */
  114.         colorpalette = cm->ColorTable;
  115.         for(i=0; i<12; i++)
  116.                 *colorpalette++ = colortable[i];
  117.                                 /* copy my colors into this data structure */
  118.         vp.ColorMap = cm;       /* link it with the viewport */
  119.  
  120.                                  /* allocate space for bitmap */
  121.         for(i=0; i<DEPTH; i++)
  122.            {
  123.            b.Planes[i] = (PLANEPTR)AllocRaster(WIDTH1,HEIGHT1);
  124.            if(b.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
  125.            b2.Planes[i] = (PLANEPTR)AllocRaster(WIDTH,HEIGHT);
  126.            if(b2.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
  127.            }
  128.  
  129.         MakeVPort( &v, &vp );   /* construct copper instr (prelim) list */
  130.         MrgCop( &v );           /* merge prelim lists together into a real 
  131.                                  * copper list in the view structure. */
  132.  
  133.         for(i=0; i<2; i++)
  134.                 {
  135.                 displaymem = (UBYTE *)b.Planes[i];
  136.                 displaymem2 = (UBYTE *)b2.Planes[i];
  137.                 for(j=0; j<RASSIZE(WIDTH1,HEIGHT1); j++)
  138.                         {       
  139.                         *displaymem++ = 0;      
  140.                         }
  141.                 for(j=0; j<RASSIZE(WIDTH,HEIGHT); j++)
  142.                         {       
  143.                         *displaymem2++ = 0;     
  144.                         }
  145.                 /* zeros to all bytes of the display area */                                    }
  146.  
  147.         LoadView(&v);
  148.  
  149.         /* now fill some boxes so that user can see something */
  150.         /* always draw into both planes to assure true colors */
  151.  
  152.         for(n=1; n<4; n++)      /* three boxes */
  153.         {
  154.                 for(k=0; k<2; k++)
  155.                 {
  156.                 displaymem = b.Planes[k] + boxoffsets[n-1];  
  157.                 DrawFilledBox(n,k,displaymem,&b);       
  158.                 displaymem2 = b2.Planes[k] + boxoffsets2[n-1];  
  159.                 DrawFilledBox(n,k,displaymem2,&b2);     
  160.                 }       
  161.         }
  162.         /* now tear a hole in the playfield so user can see that
  163.          * foreground area of playfield 2 (called PFB also) is
  164.          * transparent in any area where it has a color value of 0
  165.          */
  166.         for(k=0;k<2;k++)
  167.         {
  168.                 displaymem = b2.Planes[k] + (UBYTE *)1826;
  169.                 CutAHole(6,40,displaymem);
  170.         }
  171.         for(k=0;k<2;k++)
  172.         {
  173.                 displaymem = b2.Planes[k] + (UBYTE *)4204;
  174.                 CutAHole(3,40,displaymem);
  175.         }
  176.         for(k=0;k<2;k++)
  177.         {
  178.                 displaymem = b2.Planes[k] + (UBYTE *)619;
  179.                 CutAHole(8,20,displaymem);
  180.         }
  181.         for(k=0;k<2;k++)
  182.         {
  183.                 displaymem = b2.Planes[k] + (UBYTE *)1809;
  184.                 CutAHole(3,10,displaymem);
  185.         }
  186.  
  187.  
  188.         scrollit();
  189.  
  190.         FreeMemory();   /* discussion purposes only, user has no 
  191.                          * way here to get past "FOREVER" in this
  192.                          * simple program.  FreeMemory is discussed
  193.                          * in section below... Exiting Gracefully */
  194.  
  195. }       /* end of main() */
  196.  
  197. DrawFilledBox(fillcolor,plane,where,bm)
  198. SHORT fillcolor,plane;
  199. UBYTE *where;
  200. struct BitMap *bm;
  201. {
  202.         UBYTE value;
  203.         
  204.         for(j=0; j<100; j++)
  205.            {
  206.            if((fillcolor & (1 << plane)) != 0)
  207.                 value = 0xff;
  208.            else
  209.                 value = 0;
  210.            for(i=0; i<20; i++)
  211.                 {
  212.                 *where++ = value;
  213.                 }
  214.            where += ((bm->BytesPerRow) - 20);
  215.            }
  216.         return;
  217. }
  218.  
  219. CutAHole(width,height,startatbyte)
  220. SHORT width,height;
  221. UBYTE *startatbyte;
  222. {
  223.         for(j=0; j<height; j++)
  224.         {
  225.                 for(i=0; i<width; i++)
  226.                         *startatbyte++ = 0;
  227.                 startatbyte += b2.BytesPerRow - width;
  228.         }               
  229.         return;
  230. }
  231.  
  232. FreeMemory()
  233. {               /* return user and system-allocated memory to sys manager */
  234.  
  235.         for(i=0; i<DEPTH; i++)                  /* free the drawing area */
  236.            FreeRaster(b.Planes[i],WIDTH,HEIGHT);
  237.         FreeColorMap(cm);                       /* free the color map */
  238.                 /* free dynamically created structures */
  239.         FreeVPortCopLists(&vp);                 
  240.         FreeCprList(&v.LOFCprList);
  241.         return;
  242. }
  243.  
  244.         
  245.  
  246. /* this is a set of pointers which will be used to hold the old values
  247.    of these pointers .... we reuse an old copper list by saying makeview
  248.    with whatever copper list is currently in LOFCprList and SHFCprList.
  249.    If they are currently = 0, a brand new copper list is created 
  250.    (dynamically allocated.... should be deallocated on the way out
  251.     using dAllocCp()) */ 
  252.  
  253. struct cprlist *LOF[2];
  254. struct cprlist *SHF[2];
  255.  
  256. extern struct RasInfo ri;
  257. extern struct View v;
  258. extern struct ViewPort vp;
  259.  
  260. short w;
  261.  
  262.  
  263. scrollit()
  264. {
  265.         short i,j;
  266.  
  267.         /* graphics library is already opened during main() */
  268.  
  269.         w=0;
  270.  
  271.         /* because these are null, first application creates a new list */
  272.         LOF[1] = NULL;  
  273.         SHF[1] = NULL;
  274.  
  275.     for(;;)             /* forever */
  276.     {
  277.     for(i=0; i<MAXSCROLL_X; i++)
  278.         {
  279.         swapPointers();
  280.         ri.RxOffset++;
  281.                 /* give it a new offset value, so as to scroll the
  282.                         display */
  283.         remakeView();
  284.         w ^= 1;         /* exclusive-or to swap pointers */
  285.         }       
  286.     for(i=0; i<MAXSCROLL_Y; i++)
  287.         {
  288.         swapPointers(); ri.RyOffset++; remakeView(); w ^= 1;            
  289.         }
  290.     for(i=MAXSCROLL_X; i>(0); i--)
  291.         {
  292.         swapPointers(); ri.RxOffset--; remakeView(); w ^= 1;            
  293.         }
  294.     for(i=MAXSCROLL_Y; i>(0); i--)
  295.         {
  296.         swapPointers(); ri.RyOffset-- ; remakeView(); w ^= 1;           
  297.         }
  298.     }           /* end of forever */
  299.  
  300. }       /* end of scrollit() */
  301.  
  302.  
  303. swapPointers()          /* provided for double buffering of copper lists */
  304. {
  305.         LOF[w] = v.LOFCprList;
  306.         SHF[w] = v.SHFCprList;
  307.         v.LOFCprList = LOF[(w^1)];
  308.         v.SHFCprList = SHF[(w^1)];
  309.  
  310.         /* swap the pointers so that they can reuse existing space */
  311.         return;
  312. }
  313.  
  314. remakeView()
  315. {
  316.         MakeVPort(&v, &vp);
  317.         MrgCop(&v);
  318.         LoadView(&v);           /* and show it */
  319.         WaitTOF();              /* slow things down so we can see it move */
  320.         return;
  321. }
  322.  
  323.